﻿using HalconDotNet;
using MachineVision.Core.TemplateMatch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MachineVision.Core.OCR
{
    public class QrCodeService
    {
        public QrCodeService()
        {
            HOperatorSet.CreateDataCode2dModel("QR Code", new HTuple(), new HTuple(), out hv_DataCodeHandle);
            Info = new MethodInfo()
            {
                Name = "find_data_code_2d",
                Description = "Detect and read 2D data code symbols in an image or train the 2D data code model.",
                Parameters = new List<MathodParameter>
                {
                    new MathodParameter(){ Name = "Image", Description = "Input image. If the image has a reduced domain, the data code search is reduced to that domain. This usually reduces the runtime of the operator. However, if the datacode is not fully inside the domain, the datacode might not be found correctly. In rare cases, data codes may be found outside the domain. If these results are undesirable, they have to be subsequently eliminated." },
                    new MathodParameter(){ Name = "SymbolXLDs", Description = "XLD contours that surround the successfully decoded data code symbols. The order of the contour points reflects the orientation of the detected symbols. The contours begin in the top left corner (see 'orientation' at get_data_code_2d_results) and continue clockwise." },
                    new MathodParameter(){ Name = "DataCodeHandle", Description = "Handle of the 2D data code model." },
                    new MathodParameter(){ Name = "GenParamName", Description = "Handle of the 2D data code model." },
                    new MathodParameter(){ Name = "GenParamValue", Description = "Values of the optional generic parameters." },
                    new MathodParameter(){ Name = "ResultHandles", Description = "Handles of all successfully decoded 2D data code symbols." },
                    new MathodParameter(){ Name = "DecodedDataStrings", Description = "Decoded data strings of all detected 2D data code symbols in the image." },
                },
                Predecessors = new List<String>
                {
                    "get_data_code_2d_results",
                    "get_data_code_2d_objects",
                    "write_data_code_2d_model",
                }
            };
        }

        HObject ho_SymbolXLDs = null;
        HTuple hv_ImageFiles = new HTuple();
        HTuple hv_Message = new HTuple();
        HTuple hv_DataCodeHandle = new HTuple(), hv_Index = new HTuple();
        HTuple hv_ResultHandles = new HTuple(), hv_DecodedDataStrings = new HTuple();

        public MethodInfo Info { get; set; }

        public RoiParameter Roi { get; set; }

        public void Run (HObject Image)
        {
            ho_SymbolXLDs.Dispose(); hv_ResultHandles.Dispose(); hv_DecodedDataStrings.Dispose();
            HOperatorSet.FindDataCode2d(Image, out ho_SymbolXLDs, hv_DataCodeHandle,
                new HTuple(), new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
        }
    }
}
